﻿using AutoMapper;
using IOA.MES.DataAccess.Entity;
using IOA.MES.DataAccess.VModel;
using IOA.MES.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOA.MES.Business
{
    /// <summary>
    /// 组件过站配置（错误代码：128001）
    /// </summary>
    public class CompCrossSiteMgr
    {
        private MESContext mesDb = new MESContext("MES");

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="model">组件过站配置</param>
        /// <returns>组件过站配置</returns>
        public VCompCrossSite Edit(VCompCrossSite model)
        {
            if (model.PkId <= 0)
            {
                model.CreateTime = DateTime.Now;
                model.ModifyBy = 0;
                var entity = Mapper.Map<CompCrossSite>(model);
                mesDb.CompCrossSites.Add(entity);
                mesDb.SaveChanges();

                model = Mapper.Map<VCompCrossSite>(entity);
            }
            else
            {
                var entity = mesDb.CompCrossSites.FirstOrDefault(_ => _.PkId == model.PkId && _.Valid);
                if (entity == null)
                {
                    throw new MultiLanBizException(128001);// "待编辑的记录不存在或已被删除，无法编辑");
                }
                entity.OrderNo = !string.IsNullOrEmpty(model.OrderNo) ? model.OrderNo.Trim() : null;
                entity.FragRepeat = model.FragRepeat;
                entity.FragNoSite = model.FragNoSite;
                entity.StackRepeat = model.StackRepeat;
                entity.StackNoSite = model.StackNoSite;
                entity.FrameRepeat = model.FrameRepeat;
                entity.FrameNoSite = model.FrameNoSite;
                entity.WeldRepeat = model.WeldRepeat;
                entity.WeldNoSite = model.WeldNoSite;
                entity.FramePhoto = model.FramePhoto;
                entity.FrameNoScan = model.FrameNoScan;
                entity.ModifyBy = model.ModifyBy;
                entity.ModifyTime = DateTime.Now;
                mesDb.SaveChanges();

                model = Mapper.Map<VCompCrossSite>(entity);
            }
            return model;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public VCompCrossSite Get(string OrderNo)
        {
            VCompCrossSite model = null;
            var entity = mesDb.CompCrossSites.FirstOrDefault(_ => _.Valid && _.OrderNo.Equals(OrderNo));
            if (entity == null)
            {
                model = new VCompCrossSite();
            }
            else
            {
                model = new VCompCrossSite
                {
                    PkId = entity.PkId,
                    OrderNo = !string.IsNullOrEmpty(entity.OrderNo) ? entity.OrderNo.Trim() : null,
                    FragRepeat = entity.FragRepeat,
                    FragNoSite = entity.FragNoSite,
                    WeldRepeat = entity.WeldRepeat,
                    WeldNoSite = entity.WeldNoSite,
                    FrameRepeat = entity.FrameRepeat,
                    FrameNoSite = entity.FrameNoSite,
                    StackRepeat = entity.StackRepeat,
                    StackNoSite = entity.StackNoSite,
                    FramePhoto = entity.FramePhoto,
                    FrameNoScan = entity.FrameNoScan
                };
            }
            return model;
        }
    }
}
